Completed
Push — master ( d83c14...87c801 )
by Jan
16s queued 16s
created

create.ts ➔ create   A

Complexity

Conditions 2

Size

Total Lines 5
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 15
dl 0
loc 5
rs 9.65
c 0
b 0
f 0
1
import * as ID3Util from "../ID3Util"
2
import { WriteTags } from "../types/Tags"
3
import { isFunction } from  "../util"
4
import { createBufferFromTags } from "../TagsHelpers"
5
6
export type CreateCallback =
7
    (data: Buffer) => void
8
9
/**
10
 * Creates a buffer containing the ID3 Tag
11
 */
12
export function create(tags: WriteTags): Buffer
13
export function create(tags: WriteTags, callback: CreateCallback): void
14
export function create(tags: WriteTags, callback?: CreateCallback) {
15
    const frames = createBufferFromTags(tags)
16
17
    //  Create ID3 header
18
    const header = Buffer.alloc(10)
19
    header.fill(0)
20
    header.write("ID3", 0)              //File identifier
21
    header.writeUInt16BE(0x0300, 3)     //Version 2.3.0  --  03 00
22
    header.writeUInt16BE(0x0000, 5)     //Flags 00
23
    ID3Util.encodeSize(frames.length).copy(header, 6)
24
25
    const id3Data = Buffer.concat([header, frames])
26
27
    if (isFunction(callback)) {
28
        return callback(id3Data)
29
    }
30
    return id3Data
31
}
32